package com.coffeeloversupportlibrary.coffeelover.mercury.login;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.core.widget.TextViewCompat;
import androidx.fragment.app.Fragment;


import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.domain.MainActivity;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.User;

import com.coffeeloversupportlibrary.coffeelover.mercury.utils.HttpUtils;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.JsonUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @author 盛守义
 * @createTime 2021/10/24 15:04
 * @projectName TurningCoffee
 * @className LoginActivity.java
 * @description TODO
 */
public class LoginActivity extends AppCompatActivity {
    private View view;
    private OnButtonClick onButtonClick;

    private EditText et_email,et_pwd;
    private AppCompatButton btn_forget, btn_regist, btn_submit;
    private String email, pwd;
    private Intent intent;
    private Activity activity = LoginActivity.this;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        setListener();
    }

    void initViews(){
        et_email = findViewById(R.id.et_emaillogin);
        et_pwd = findViewById(R.id.et_pwdlogin);
        btn_forget = findViewById(R.id.btn_forgetlogin);
        btn_regist = findViewById(R.id.btn_rgstlogin);
        btn_submit = findViewById(R.id.btn_submitlogin);
    }

    class MyClickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btn_forgetlogin:
                    intent =new Intent(activity, ForgetPwdActivity.class);
                    startActivity(intent);
                    break;
                case R.id.btn_rgstlogin:
                    intent =new Intent(activity, RegistActivity.class);
                    startActivity(intent);
                    LoginActivity.this.finish();
                    break;
                case R.id.btn_submitlogin:
                    getValue();
                    if(email.equals("")||email == null||pwd.equals("")||pwd == null){
                        Toast.makeText(LoginActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                    }else{
                        Gson gson = new Gson();

                        JSONObject jsonObject = new JSONObject();
                        try {
                            jsonObject.put("userTel", email);
                            jsonObject.put("userPassword", pwd);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        String jsonString = jsonObject.toString();
                        Log.e("LoginActivity:", "jsonString:"+jsonString);
                        //发送用户名密码并接收
                        HttpUtils httpUtils = new HttpUtils();
                        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), jsonString);

                        //输入url
                        Call call = httpUtils.getCall(requestBody, "/user/login");
                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                                LoginActivity.this.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(LoginActivity.this, "网络连接崩溃了，绝对绝对不是我们的错", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }

                            @Override
                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                String res = response.body().string();
                                Log.e( "LoginActivity:","返回信息:"+ res);
                                User user = new JsonUtils().getUser(res);

                                LoginActivity.this.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if(user.getSuccess() == true){
                                            intent = new Intent(activity, MainActivity.class);

                                            SharedPreferences userShare = getSharedPreferences("user", 0);
                                            SharedPreferences.Editor editor = userShare.edit();
                                            editor.putString("userInfo", res);
                                            editor.putInt("coffeeBean", user.getData().getUser().getCoffeeBean());
                                            editor.commit();

                                            startActivity(intent);
                                        }
                                    }
                                });
                            }
                        });
                    }
                    break;
            }
        }
    }

    void setListener(){
        MyClickListener listener = new MyClickListener();
        btn_forget.setOnClickListener(listener);
        btn_regist.setOnClickListener(listener);
        btn_submit.setOnClickListener(listener);
    }

    void getValue(){
        email = et_email.getText().toString().trim();
        pwd = et_pwd.getText().toString().trim();
    }

    public interface OnButtonClick{
        public void onClick(View view);
    }

    public OnButtonClick getOnButtonClick() {
        return onButtonClick;
    }
    //定义接口变量的set方法
    public void setOnButtonClick(OnButtonClick onButtonClick) {
        this.onButtonClick = onButtonClick;
    }
}
